package com.leinbo.onedoc.kpojo

/**
 * @author  Richard
 * @date  2022/3/23 13:39
 */
private const val SUCCESS = 200
private const val FAIL = 0
const val CODE = "code"
const val DATA = "data"
const val MSG = "msg"

data class Response(val code: Int, val msg: String?, val data: Any?) : HashMap<String?, Any?>() {
    init {
        super.put(CODE, code)
        msg?.let {
            super.put(MSG, it)
        }

        data?.let {
            super.put(DATA, it)
        }
    }

    /**
     * 方便链式调用
     *
     * @param key 键
     * @param value 值
     * @return 数据对象
     */
    override fun put(key: String?, value: Any?): Response {
        super.put(key, value)
        return this
    }

    fun put(map: Map<String, Any?>): Response {
        super.putAll(map)
        return this
    }

    fun put(vararg params: Pair<String, Any?>): Response {
        super.putAll(params.toMap())
        return this
    }

    companion object {

        /**
         * 返回成功消息
         *
         * @param msg 返回内容
         * @return 成功消息
         */
        fun success(data: Any? = null, msg: String = "请求成功！"): Response {
            return Response(SUCCESS, msg, data)
        }

        /**
         * 返回错误消息
         *
         * @param msg 返回结果
         * @return 警告消息
         */
        fun error(msg: String = "请求失败！"): Response {
            return Response(FAIL, msg, null)
        }
    }
}
